﻿var express = require('express');
var router = express.Router();	
var db = require('../dao/categoryDao');
var site_type =	config = require("../config").site_type;

var renderIndex = function(req, res, categories, condition){
    res.render('index.html', { 
        pageTitle: 'Express',
        controllerName: "category",
        siteType: site_type,
        curType: site_type.category,
        todos: categories,
        searchCondition: condition
    });

};
exports.index = function(req, res, next){
    var condition = {
        keyword: req.query.keyword || '',
        title: req.query.title || ''
    };
	db.findAll(function (err, categories) {
        if (err) {
            return next(err);
        }
        if(categories==null || categories.length==0){   
            db.add({title:"默认分类", is_edit:false, order:99}, function(err, categoryEntity){
                categories=[categoryEntity];
                renderIndex(req, res, categories, condition);
            });
        }
        else{
            renderIndex(req, res, categories, condition);
        }
    });
};

exports.edit = function (req, res, next) {
    var id = req.params.id;
    if(id == 0){
        res.render('edit.html', {
            siteType: site_type,
            curType: site_type.category,
            todo: {_id:id, title:""}
        });
    }
    else{
        db.findById(id, function (err, doc) {
            if (err) {
                return next(err);
            }
            //console.log(doc)
            res.render('edit.html', {
                siteType: site_type,
                curType: site_type.category,
                todo: doc
            });
        });
    }
};

exports.editSubmit = function (req, res, next) {
    var id = req.body.id || 0;
    
	var title = req.body.title || '';
	title = title.trim();
    if (!title) {
        return res.render('error.html', {message: '标题是必须的'});
    }
    db.findExistByTitle(id, title, function (err, doc) {
        if (err) {
            return next(err);
        }
        else {
            console.log(doc)
            if(doc == null || doc.title != title){              
                if(id == 0){
                    db.add({title:title}, function (err, row) {
                        if (err) {
                            return next(err);
                        }
                        res.json({status:'1'});
                    });
                }
                else{
                    db.editTitle(id, title, function (err, doc) {
                        if (err) {
                            return next(err);
                        }
                        res.json({status:'1'});
                    });
                }
            }
            else{
                res.json({status:'0', msg: '标题已经存在了'});
            }
        }
    }); 

};

exports.remove = function (req, res, next) {
    var id = req.params.id;

    db.remove(id, function (err, doc) {
        if (err) {
            return next(err);
        }    

		res.json({status:'1'});        
    });

};

